package com.blb.network;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * 客户端
 */
public class MyClient {

    /**
     * 连接服务器并发送消息
     * @param ip
     * @param port
     * @param message
     */
    public void connect(String ip,int port,String message){
        //创建客户端socket
        try(Socket socket = new Socket(ip,port);
            //获得输出流，用于发消息
            ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream())
        ){
            //给服务器端发消息
            outputStream.writeUTF(message);
            //清理缓冲区
            outputStream.flush();
        }catch (IOException ex){
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new MyClient().connect("192.168.114.22",8888,"Hello!!!我是客户端！！");
    }
}
